package com.runvp.netty.runnables;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.model.tcp.MessageReceiveBody;
import com.runvp.netty.build.BuildHttpClint;
import com.runvp.netty.constant.NettyConstant;
import com.runvp.service.tcp.TcpService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DeviceResStartChargRunnable implements Runnable {

    private Channel channel;

    private TcpService tcpService;

    private MessageReceiveBody parseBody;

    public DeviceResStartChargRunnable(Channel channel, TcpService tcpService, MessageReceiveBody parseBody) {
        this.channel = channel;
        this.tcpService = tcpService;
        this.parseBody = parseBody;
    }

    @Override
    public void run() {
        handle(channel);
    }


    /**
     * 设备回应启动充电
     */
    public void handle(Channel channel) {
        long st = System.currentTimeMillis();
        long ast = st;
        String sn = "";
        try{
            String json = JSONObject.toJSONString(parseBody);
            sn = NettyConstant.allOnlineChannleIds.get(NettyConstant.getChannelId(channel));
            log.info("本次设备回应服务器发下启动充电sn: {},json: {}",sn,json);
            parseBody.setTerminalId(sn);
            BuildHttpClint.postJson("/app/cmd/device/res/start/charg",parseBody);
        }catch (Exception e){
            log.error("本次设备回应启动充电【{}】处理异常共耗时:【{} ms】,error:{}",sn,(System.currentTimeMillis()-ast),e.getMessage());
        }finally {
            log.info("本次设备回应启动充电【{}】线程池内解析处理共耗时:【{} ms】",sn,(System.currentTimeMillis()-ast));
        }
    }



}
